//
//  DCViewController.m
//
//  Created by 黄 隆 on 13-7-1.
//  Copyright (c) 2013年 黄 隆. All rights reserved.
//

#import "DCViewController.h"
#import "Onelong.h"

@interface DCViewController ()

@end

@implementation DCViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    //创建管理对象模型
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"codedatatest" withExtension:@"momd"];
    //_objectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    
    _objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    
    // 创建持久化数据存储协调器
    _coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:_objectModel];
    
    // 添加数据存储
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    // 创建一个SQLite数据库作为数据存储
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *filePath = [documentsDir stringByAppendingPathComponent:@"datastore.sqlite"];
    NSURL *databaseURL = [NSURL fileURLWithPath:filePath];
    
    NSError *error = nil;
    [_coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:databaseURL options:nil error:&error];
    
    // 创建托管对象上下文
    _objectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    [_objectContext setPersistentStoreCoordinator:_coordinator];
    
    // NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    //[addingContext setParentContext:_objectContext];
    
    //Onelong *newBook = (Onelong *)[NSEntityDescription insertNewObjectForEntityForName:@"Onelong" inManagedObjectContext:_objectContext];
    //[newBook setName:@"iamonelong"];
    //if ([_objectContext hasChanges]&&[[self objectContext] save:nil]) {
    //NSLog(@"ok");
    //}
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Onelong" inManagedObjectContext:_objectContext];
    [fetchRequest setEntity:entity];
    
    //NSPredicate *fredicate = [NSPredicate predicateWithFormat: @"name == 'iamonelong'"];
    //[fetchRequest setPredicate:fredicate];
    NSArray * result = [_objectContext executeFetchRequest:fetchRequest error:nil];
    for (Onelong *object in result) {
        NSLog(@"%@",[object name]);
        [object setName:@"iam ooofdfdfd"];
    }
    if ([_objectContext hasChanges]&&[[self objectContext] save:nil]) {
        //NSLog(@"ok");
    }
    
    NSString* key = @"fontsize";
    //[[NSUserDefaults standardUserDefaults]setInteger:10 forKey:key];
    // [[NSUserDefaults standardUserDefaults]setObject:@"nihao" forKey:@"fontname"];
    
    
    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:key];
    [[NSUserDefaults standardUserDefaults]registerDefaults:dictionary];
    [[NSUserDefaults standardUserDefaults]setInteger:1100 forKey:key];
    //[[NSUserDefaults standardUserDefaults]removeObjectForKey:key];
    NSString *size = [[NSUserDefaults standardUserDefaults]stringForKey:key];
    NSLog(@"%@",size);
    
    //正则表达式 1
    NSArray *arrayFilter = [NSArray arrayWithObjects:@"pict", @"blackrain", @"ip", nil];
    NSArray *arrayContents = [NSArray arrayWithObjects:@"I am a picture.", @"I am a guy", @"I am gagaga", @"ipad", @"iphone", nil];
    int i = 0,count = [arrayFilter count];
    for(i = 0; i < count; i ++)
    {
        NSString *arrayItem = (NSString *)[arrayFilter objectAtIndex:i];
        NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", arrayItem];
        NSArray *filterData = [arrayContents filteredArrayUsingPredicate:filterPredicate];
        NSLog(@"Filtered array with filter %@, %d", arrayItem, [filterData count]);
    }
    
    ////正则表达式 2
    NSArray *arrayFilter1 = [NSArray arrayWithObjects:@"abc1", @"abc2", nil];
    NSArray *arrayContent1 = [NSArray arrayWithObjects:@"a1", @"abc1", @"abc4", @"abc2", nil];
    NSPredicate *thePredicate1 = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrayFilter1];
    NSLog(@"%@",[arrayContent1 filteredArrayUsingPredicate:thePredicate1]);
}

-(void) test1
{
    //
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *defaultPath = [[NSBundle mainBundle] resourcePath];
    NSError *err;
    NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:defaultPath error:&err];
    //4）match的用法
    
    //1. 简单比较
    
    NSString *match = @"imagexyz-999.png";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@", match];
    NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];
    /*
    // 2. match里like的用法（类似Sql中的用法）
    NSString *match = @"imagexyz*.png";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match];
    NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];﻿
    
    //3. 大小写比较
    //［c］表示忽略大小写，［d］表示忽略重音，可以在一起使用，如下：
    NSString *match = @"imagexyz*.png";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] %@", match];
    NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];﻿
    
    
    
    //4.使用正则
    NSString *match = @"imagexyz-\\d{3}\\.png";  //imagexyz－123.png
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];﻿
     */
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
